package cn.goktech.sports.modules.importDataone.controller;

import cn.goktech.sports.common.entity.R;
import cn.goktech.sports.common.utils.ExcelUtils;
import cn.goktech.sports.modules.importDataone.service.HeadDataImportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈头程数据导入控制器〉
 *
 * @author PanY
 * @create 2019/10/29
 * @since 1.0.0
 */
@Api(value = "头程导入", description = "头程导入接口")
@RestController
@RequestMapping("/sys/headDataImport")
public class HeadDataImportController {
    @Autowired
    HeadDataImportService headDataImportService;
    /**
     * 功能描述: <br/>
     * 导入头程中转仓
     * @param file 中转仓文件
     * @author PanY
     * @date 2019/10/29 15:41
     * @return cn.goktech.sports.common.entity.R
     */
    @RequestMapping(value = "/importTransfer", method = {RequestMethod.POST})
    @ApiOperation(value = "头程仓库上传")
    public R importTransfer(@RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        if (strings == null || strings.size()==0){
            return R.error("请检查文档是否有数据");
        }
        Map<String,Object> map = new HashMap<>();
        Integer success = 0;
        Integer updates = 0;
        for (int i = 0; i < strings.size(); i++) {
            //验证数据是否存在，在进行修改或新增
            //第一次取出的是标题：【中转仓名称、描述】
            if (i == 0) {
                if(!"仓库名称".equals(strings.get(i)[0]) || !"描述".equals(strings.get(i)[1])){
                    return R.error("请选择正确的仓库文件！");
                }
                continue;
            }
            String[] forstrings = strings.get(i);
            if (forstrings[0] == null || StringUtils.isBlank(forstrings[0]))continue;
            Map<String, String> stringStringMap = headDataImportService.queryBaseTransfer(forstrings[0]);
            map.put("transfer_name",forstrings[0]);
            map.put("remark",forstrings[1]);
            if ( stringStringMap == null ){
                //新增
                headDataImportService.addBaseTransfer(map);
                success++;
            }else {
                //修改
                map.put("transfer_id",stringStringMap.get("transfer_id"));
                headDataImportService.updateBaseTransfer(map);
                updates++;
            }
        }
        return R.ok("导入成功！\n成功添加："+success+"\n成功修改："+updates);
    }
    /**
     * 功能描述: <br/>
     * 导入头程尺寸标准
     * @param file 尺寸文件
     * @author PanY
     * @date 2019/10/30 11:14
     * @return cn.goktech.sports.common.entity.R
     */
    @RequestMapping(value = "/importHeadSize", method = {RequestMethod.POST})
    @ApiOperation(value = "头程尺寸标准上传")
    public R importHeadSize(@RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        if (strings == null || strings.size()==0){
            return R.error("请检查文档是否有数据");
        }
        Map<String,Object> param = new HashMap<>();
        Integer success = 0;
        Integer updates = 0;
        for (int i = 0; i < strings.size(); i++) {
            //跳过标题
            if (i == 0){
                if(!"最长边".equals(strings.get(i)[0]) || !"次边长".equals(strings.get(i)[1])){
                    return R.error("请选择正确的头程尺寸文件");
                }
                continue;
            }
            //数据
            String[] forstrings = strings.get(i);
            //验证数据是否为空，遇到最开始写了数据然后删除了这种数据判断起作用
            if (forstrings[0] == null || StringUtils.isBlank(forstrings[0]))continue;

            param.put("max_long",forstrings[0]);
            param.put("max_width",forstrings[1]);
            param.put("surpass_volume",forstrings[2]);
            param.put("surpass_weight",forstrings[3]);
            param.put("identity",forstrings[4]);    //尺寸标识，为了后面导入头程数据时，根据标识来查询尺寸并进行关联
            Map<String, Object> objectMap = headDataImportService.queryHeaderStand(param);
            if (objectMap == null){
                //新增
                headDataImportService.addHeaderStand(param);
                success++;
            }else {
                param.put("header_stand_id",objectMap.get("header_stand_id"));
                //修改
                headDataImportService.updateHeaderStand(param);
                updates++;
            }
        }
        return R.ok("导入成功！\n成功添加："+success+"\n成功修改："+updates);
    }
    /**
     * 功能描述: <br/>
     * 导入头程数据
     * @param file 数据文件
     * @author PanY
     * @date 2019/10/30 11:14
     * @return cn.goktech.sports.common.entity.R
     */
    @RequestMapping(value = "/importHeaderWay", method = {RequestMethod.POST})
    @ApiOperation(value = "头程数据上传")
    public R importHeaderWay(@RequestParam("file") MultipartFile file) throws IOException{
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        if (strings == null || strings.size()==0){
            return R.error("请检查文档是否有数据");
        }
        Map<String,Object> param = new HashMap<>();
        Integer success = 0;
        Integer updates = 0;
        for (int i = 0; i < strings.size(); i++) {
            //跳过标题
            if (i == 0) {
                if (!"发往仓库名称".equals(strings.get(i)[0]) || !"服务类型".equals(strings.get(i)[1]) ){
                    return R.error("请选择正确的数据文件!");
                }
                continue;
            }
            //数据
            String[] forstrings = strings.get(i);
            int length = forstrings.length;
            //验证数据是否为空，遇到最开始写了数据然后删除了这种数据判断起作用
            if (forstrings[0] == null || StringUtils.isBlank(forstrings[0])) continue;
            //根据尺寸标识查询尺寸，关联时需要尺寸id
            param.put("identity",length >=8 ?forstrings[7]:"");
            Map<String, Object> stringObjectMap = headDataImportService.queryHeaderStandByIdentity(param);
            /*if (stringObjectMap == null){
                return R.error("服务类型："+forstrings[1]+"的尺寸标识不存在，请先添加尺寸！");
            }*/
            //验证中转仓
            Map<String, String> stringStringMap = headDataImportService.queryBaseTransfer(forstrings[6]);
            if (stringStringMap == null){
                return R.error("中转仓："+forstrings[6]+"不存在，请先添加中转仓！");
            }
            param.put("storehouse_name",forstrings[0]);
            param.put("service_type",forstrings[1]);
            param.put("reference_aging",forstrings[2]);
            param.put("weight_volume",forstrings[3]);
            param.put("billing_weight",forstrings[4]);
            param.put("is_fuel_cost",forstrings[5]);
            param.put("transfer_id",stringStringMap.get("transfer_id"));
            param.put("header_stand_id",stringObjectMap.get("header_stand_id")==null?"":stringObjectMap.get("header_stand_id"));
            param.put("fuel_type",length>8?forstrings[8]:"");   //燃油标识
            //如果燃油不为空
            Map<String, Object> way = headDataImportService.queryHeaderFuel(param);
            if (way != null){
                param.put("fuel_id",way.get("fuel_id"));
            }
            //查询头程数据，判断是否存在
            Map<String, Object> headerWay = headDataImportService.queryHeaderWay(param);
            if (headerWay == null){
                //新增
                headDataImportService.addHeaderWay(param);
                success++;
            }else{
                param.put("header_id",headerWay.get("header_id"));
                //修改
                headDataImportService.updateHeaderWay(param);
                updates++;
            }
        }
        return R.ok("导入成功！\n成功添加："+success+"\n成功修改："+updates);
    }

    /**
     * 功能描述: <br>
     * 陆运数据导入
     * @param file
     * @author PanY
     * @date 2019/11/7 10:03
     * @return cn.goktech.sports.common.entity.R
     */
    @RequestMapping(value = "/importLandCarriage", method = {RequestMethod.POST})
    @ApiOperation(value = "陆运数据上传")
    public R importLandCarriage(@RequestParam("file") MultipartFile file) throws IOException{
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        if (strings == null || strings.size()==0){
            return R.error("请检查文档是否有数据");
        }
        Map<String,Object> param = new HashMap<>();
        Integer success = 0;
        Integer updates = 0;
        for (int i = 0; i < strings.size(); i++) {
            //跳过标题
            if (i == 0) {
                if (!"发往仓库名称".equals(strings.get(i)[0]) || !"服务类型".equals(strings.get(i)[1])) {
                    return R.error("请选择正确的数据文件!");
                }
                continue;
            }
            //数据
            String[] forstrings = strings.get(i);
            //验证数据是否为空，遇到最开始写了数据然后删除了这种数据判断起作用
            if (forstrings[0] == null || StringUtils.isBlank(forstrings[0])) continue;
            if (StringUtils.isBlank(forstrings[5])){
                return R.error("数据不正确，请填写正确的数据！");
            }
            //验证中转仓
            Map<String, String> stringStringMap = headDataImportService.queryBaseTransfer(forstrings[5]);
            if (stringStringMap == null){
                return R.error("中转仓："+forstrings[5]+"不存在，请先添加中转仓！");
            }
            param.put("storehouse_name",forstrings[0]);
            param.put("service_type",forstrings[1]);
            param.put("reference_aging",forstrings[2]);
            param.put("weight_volume",forstrings[3]);
            param.put("billing_weight",forstrings[4]);
            param.put("is_fuel_cost",0);        //固定燃油为0
            param.put("transfer_id",stringStringMap.get("transfer_id"));
            //查询头程数据，判断是否存在
            Map<String, Object> headerWay = headDataImportService.queryHeaderLandTransport(param);
            if (headerWay == null){
                //新增
                headDataImportService.addLandCarriage(param);
                success++;
            }else{
                param.put("header_id",headerWay.get("header_id"));
                //修改
                headDataImportService.updateLandCarriage(param);
                updates++;
            }
        }
        return R.ok("导入成功！\n成功添加："+success+"\n成功修改："+updates);
    }
    /**
     * 功能描述: <br/>
     * 导入头程计费区间
     * @param file 头程计费区间文件
     * @author PanY
     * @date 2019/10/31 10:01
     * @return cn.goktech.sports.common.entity.R
     */
    @RequestMapping(value = "/importHeaderAirWeight", method = {RequestMethod.POST})
    @ApiOperation(value = "头程计费区间数据上传")
    public R importHeaderAirWeight(@RequestParam("file") MultipartFile file) throws IOException{
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        if (strings == null || strings.size()==0){
            return R.error("请检查文档是否有数据");
        }
        Map<String,Object> param = new HashMap<>();
        Integer success = 0;
        Integer updates = 0;
        for (int i = 0; i < strings.size(); i++) {
            param.clear();
            //跳过标题
            if (i == 0) {
                if (!"最小重量".equals(strings.get(i)[0]) || !"最大重量".equals(strings.get(i)[1]) ){
                    return R.error("请选择正确的计费区间文件！");
                }
                continue;
            }
            //数据
            String[] forstrings = strings.get(i);
            //验证数据是否为空，遇到最开始写了数据然后删除了这种数据判断起作用
            if (forstrings[0] == null || StringUtils.isBlank(forstrings[0])) continue;
            param.put("service_type",forstrings[3]);
            //根据头程名称查询头程是否存在
            List<Map<String, Object>> maps = headDataImportService.queryHeaderWayByServiceType(param);
            if (maps.size() == 0){
                return R.error("头程数据："+forstrings[3]+"不存在，请添加头程数据");
            }
            //头程可能有重复的。
            for (Map<String, Object> ss: maps){
                param.put("min_weight",forstrings[0]);
                if ( !StringUtils.isBlank(forstrings[1]) ){
                    param.put("max_weight",forstrings[1]);
                }
                param.put("price",forstrings[2]);
                param.put("head_name",forstrings[3]);
                param.put("header_id",ss.get("header_id"));
                Map<String, Object> stringObjectMap = headDataImportService.queryHeaderAirWeight(param);
                if (stringObjectMap == null){
                    //新增
                    headDataImportService.addHeaderAirWeight(param);
                    success++;
                }else {
                    //修改
                    param.put("air_weight_id",stringObjectMap.get("air_weight_id"));
                    headDataImportService.updateHeaderAirWeight(param);
                    updates++;
                }
            }
        }
        return R.ok("导入成功！\n成功添加："+success+"\n成功修改："+updates);
    }
    /**
     * 功能描述: <br/>
     * 海运数据导入
     * @param file 文件
     * @author PanY
     * @date 2019/10/31 15:15
     * @return cn.goktech.sports.common.entity.R
     */
    @RequestMapping(value = "/importHeaderSea", method = {RequestMethod.POST})
    @ApiOperation(value = "海运数据导入")
    public R importHeaderSea(@RequestParam("file") MultipartFile file) throws IOException{
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        if (strings == null || strings.size()==0){
            return R.error("请检查文档是否有数据");
        }
        Map<String,Object> param = new HashMap<>();
        Integer success = 0;
        Integer updates = 0;
        for (int i = 0; i < strings.size(); i++) {
            //跳过标题
            if (i == 0) {
                if ( !"发往仓库名称".equals(strings.get(i)[0]) || !"海运名称".equals(strings.get(i)[1]) ){
                    return R.error("请选择正确的海运数据！");
                }
                continue;
            }
            //数据
            String[] forstrings = strings.get(i);
            //验证数据是否为空，遇到最开始写了数据然后删除了这种数据判断起作用
            if (forstrings[0] == null || StringUtils.isBlank(forstrings[0])) continue;
            //验证中转仓是否存在
            Map<String, String> stringStringMap = headDataImportService.queryBaseTransfer(forstrings[5]);
            if (stringStringMap == null){
                return R.error("中转仓："+forstrings[5]+"不存在，请先添加中转仓！");
            }
            //判断是否已经有了该海运
            param.put("transfer_name",forstrings[5]);
            param.put("warehouse_name",forstrings[0]);
            param.put("shipping_name",forstrings[1]);
            param.put("unit",forstrings[2]);
            param.put("purpose_area",forstrings[3]);
            param.put("volume_claim",forstrings[4]);
            param.put("transfer_id",stringStringMap.get("transfer_id"));
            param.put("recent_time",forstrings[6]);
            Map<String, Object> stringObjectMap = headDataImportService.queryHeaderSea(param);
            if (stringObjectMap == null){
                //新增
                headDataImportService.addHeaderSea(param);
                success++;
            }else {
                param.put("sea_id",stringObjectMap.get("sea_id"));
                //修改
                headDataImportService.updateHeaderSea(param);
                updates++;
            }
        }
        return R.ok("导入成功！\n成功添加："+success+"\n成功修改："+updates);
    }
    /**
     * 功能描述: <br/>
     * 海运区间数据导入
     * @param file 区间数据文件
     * @author PanY
     * @date 2019/10/31 16:25
     * @return cn.goktech.sports.common.entity.R
     */
    @RequestMapping(value = "/importShippingCharging", method = {RequestMethod.POST})
    @ApiOperation(value = "海运区间数据导入")
    public R importShippingCharging(@RequestParam("file") MultipartFile file) throws IOException{
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        if (strings == null || strings.size()==0){
            return R.error("请检查文档是否有数据");
        }
        Map<String,Object> param = new HashMap<>();
        Integer success = 0;
        Integer updates = 0;
        for (int i = 0; i < strings.size(); i++) {
            param.clear();
            //跳过标题
            if (i == 0) {
                if( !"最小立方".equals(strings.get(i)[0]) || !"最大立方".equals(strings.get(i)[1]) ){
                    return R.error("请选择正确的海运计费区间数据");
                }
                continue;
            }
            //数据
            String[] forstrings = strings.get(i);
            int length = forstrings.length;
            //验证数据是否为空，遇到最开始写了数据然后删除了这种数据判断起作用
            if ( (forstrings[0] == null || StringUtils.isBlank(forstrings[0])) && (forstrings[2] == null || StringUtils.isBlank(forstrings[2])) ) continue;
            //验证海运是否存在
            Map<String,Object> map = new HashMap<>();
            map.put("shipping_name",length>=6?forstrings[5]:"");
            List<Map<String, Object>> maps = headDataImportService.queryHeaderSeaByServiceType(map);
            if (maps.size() == 0){
                return R.error("海运："+forstrings[5]+"不存在，请添加海运数据");
            }
            //海运存在，添加计费数据(可能不止一个海运，所以采用循环)
            for (Map<String, Object> m:maps){

                if ( !StringUtils.isBlank(forstrings[0]) ){
                    param.put("min_weight",forstrings[0]);
                }
                if ( !StringUtils.isBlank(forstrings[1])){
                    param.put("max_weight",forstrings[1]);
                }
                if ( !StringUtils.isBlank(forstrings[2])){
                    param.put("shipping_price",forstrings[2]);
                }
                if ( !StringUtils.isBlank(forstrings[3])){
                    param.put("shipping_bulk_weight",forstrings[3]);
                }
                if ( !StringUtils.isBlank(forstrings[4])){
                    param.put("unit",forstrings[4]);
                }
                param.put("sea_id",m.get("sea_id"));
                Map<String, Object> stringObjectMap = headDataImportService.queryShippingCharging(param);
                if (stringObjectMap == null){
                    //新增
                    headDataImportService.addShippingCharging(param);
                    success++;
                }else {
                    //修改
                    param.put("shipping_weight_id",stringObjectMap.get("shipping_weight_id"));
                    headDataImportService.updateShippingCharging(param);
                    updates++;
                }
            }
        }
        return R.ok("导入成功！\n成功添加："+success+"\n成功修改："+updates);
    }

    /**
     * 功能描述: <br/>
     * 铁运数据导入
     * @param file 铁运数据文档
     * @author PanY
     * @date 2019/10/31 17:21
     * @return cn.goktech.sports.common.entity.R
     */
    @RequestMapping(value = "/importHeaderRailway", method = {RequestMethod.POST})
    @ApiOperation(value = "铁运数据导入")
    public R importHeaderRailway(@RequestParam("file") MultipartFile file) throws IOException{
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        if (strings == null || strings.size()==0){
            return R.error("请检查文档是否有数据");
        }
        Map<String,Object> param = new HashMap<>();
        Integer success = 0;
        Integer updates = 0;
        for (int i = 0; i < strings.size(); i++) {
            //跳过标题
            if (i == 0){
                if (!"通往仓库名称".equals(strings.get(i)[0]) || !"铁运名称".equals(strings.get(i)[1])){
                    return R.error("请选择正确的铁运数据");
                }
                continue;
            }
            //数据
            String[] forstrings = strings.get(i);
            //验证数据是否为空，遇到最开始写了数据然后删除了这种数据判断起作用
            if (forstrings[0] == null || StringUtils.isBlank(forstrings[0])) continue;
            //1、验证中转仓是否存在
            Map<String, String> stringStringMap = headDataImportService.queryBaseTransfer(forstrings[5]);
            if (stringStringMap == null){
                return R.error("中转仓："+forstrings[5]+"不存在，请先添加中转仓！");
            }
            //2、判断铁运数据是否已经有了
            param.put("warehouse_name",forstrings[0]);
            param.put("railway_name",forstrings[1]);
            param.put("transfer_id",stringStringMap.get("transfer_id"));
            param.put("unit",forstrings[2]);
            param.put("purpose_area",forstrings[3]);
            param.put("volume_claim",forstrings[4]);
            param.put("recent_time",forstrings[6]);
            Map<String, Object> stringObjectMap = headDataImportService.queryHeaderRailway(param);
            if (stringObjectMap == null){
                //新增
                headDataImportService.addHeaderRailway(param);
                success++;
            }else{
                //修改
                param.put("railway_id",stringObjectMap.get("railway_id"));
                headDataImportService.updateHeaderRailway(param);
                updates++;
            }
        }
        return R.ok("导入成功！\n成功添加："+success+"\n成功修改："+updates);
    }
    /**
     * 功能描述: <br/>
     * 铁运费用区间数据导入
     * @param file 区间数据文档
     * @author PanY
     * @date 2019/10/31 18:05
     * @return cn.goktech.sports.common.entity.R
     */
    @RequestMapping(value = "/importHeaderRailwaySection", method = {RequestMethod.POST})
    @ApiOperation(value = "铁运费用区间数据导入")
    public R importHeaderRailwaySection(@RequestParam("file") MultipartFile file) throws IOException{
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        if (strings == null || strings.size()==0){
            return R.error("请检查文档是否有数据");
        }
        Map<String,Object> param = new HashMap<>();
        Integer success = 0;
        Integer updates = 0;
        for (int i = 0; i < strings.size(); i++) {
            param.clear();
            //跳过标题
            if (i == 0) {
                if ( !"最小立方".equals(strings.get(i)[0]) || !"最大立方".equals(strings.get(i)[1]) ){
                    return R.error("请选择正确的铁运区间数据");
                }
                continue;
            }
            //数据
            String[] forstrings = strings.get(i);
            int length = forstrings.length;
            //验证数据是否为空，遇到最开始写了数据然后删除了这种数据判断起作用
            if (  (forstrings[0] == null || StringUtils.isBlank(forstrings[0])) && (forstrings[2] == null || StringUtils.isBlank(forstrings[2]))  ) continue;
            if (!StringUtils.isBlank(forstrings[0]))
                param.put("min_weight",forstrings[0]);
            if (!StringUtils.isBlank(forstrings[1]))
                param.put("max_weight",forstrings[1]);
            if (!StringUtils.isBlank(forstrings[2]))
                param.put("railway_price",forstrings[2]);
            if (!StringUtils.isBlank(forstrings[3]))
                param.put("railway_bulk_weight",forstrings[3]);
            if (length>=6 && !StringUtils.isBlank(forstrings[5]))
                param.put("unit",forstrings[5]);
            Map<String, Object> ssms = new HashMap<>();
            if (!StringUtils.isBlank(forstrings[4]))
                ssms.put("railway_name",forstrings[4]);
            //1、先判断铁运是否存在
            List<Map<String, Object>> maps = headDataImportService.queryHeaderRailwayByName(ssms);
            if(maps.size() == 0){
                String s = StringUtils.isBlank(forstrings[4]) ? "数据" : forstrings[4];
                return R.error("铁运："+s+"不存在，请先添加铁运数据！");
            }
            //2、判断区间记录是否存在
            for (Map<String, Object> m:maps){
                param.put("railway_id",m.get("railway_id"));//设置铁路id
                Map<String, Object> stringObjectMap = headDataImportService.queryHeaderRailwaySection(param);
                if (stringObjectMap == null){
                    //新增
                    headDataImportService.addHeaderRailwaySection(param);
                    success++;
                }else{
                    //修改
                    param.put("railway_section_id",stringObjectMap.get("railway_section_id"));
                    headDataImportService.updateHeaderRailwaySection(param);
                    updates++;
                }
            }
        }
        return R.ok("导入成功！\n成功添加："+success+"\n成功修改："+updates);
    }

    /**
     * 功能描述: <br/>
     * 陆运区间费用导入
     * @param file
     * @author PanY
     * @date 2019/11/1 10:29
     * @return cn.goktech.sports.common.entity.R
     */
    @RequestMapping(value = "/importHeaderLandWeight", method = {RequestMethod.POST})
    @ApiOperation(value = "陆运费用区间数据导入")
    public R importHeaderLandWeight(@RequestParam("file") MultipartFile file) throws IOException{
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readExcel(file);
        if (strings == null || strings.size()==0){
            return R.error("请检查文档是否有数据");
        }
        Map<String,Object> param = new HashMap<>();
        Integer success = 0;
        Integer updates = 0;
        for (int i = 0; i < strings.size(); i++) {
            //跳过标题
            if (i == 0) {
                if (!"最小重量(kg)".equals(strings.get(i)[0]) || !"最大重量(kg)".equals(strings.get(i)[1]) ){
                    return R.error("请选择正确的计费区间文件！");
                }
                continue;
            }
            //数据
            String[] forstrings = strings.get(i);
            int length = forstrings.length;
            //验证数据是否为空，遇到最开始写了数据然后删除了这种数据判断起作用
            if (forstrings[0] == null || StringUtils.isBlank(forstrings[0])) continue;
            //1、验证陆运是否存在
            param.put("head_name",forstrings[3]);
            param.put("min_weight",forstrings[0]);
            if ( !StringUtils.isBlank(forstrings[1]) ){
                param.put("max_weight",forstrings[1]);
            }else{
                param.put("max_weight","9999999");
            }
            param.put("price",forstrings[2]);
            Map<String, Object> ssms = new HashMap<>();
            ssms.put("service_type",forstrings[3]);
            //根据头程名称查询头程是否存在
            List<Map<String, Object>> maps = headDataImportService.queryHeaderWayByServiceType(ssms);
            if (maps.size() == 0){
                return R.error("头程数据："+forstrings[3]+"不存在，请添加头程数据");
            }
            //2、判断当前记录是否存在
            for (Map<String, Object> ss: maps){
                param.put("header_id",ss.get("header_id"));
                Map<String, Object> stringObjectMap = headDataImportService.queryHeaderLandWeight(param);
                if (stringObjectMap == null){
                    //新增
                    headDataImportService.addHeaderLandWeight(param);
                    success++;
                }else {
                    //修改
                    param.put("air_weight_id",stringObjectMap.get("air_weight_id"));
                    headDataImportService.updateHeaderLandWeight(param);
                    updates++;
                }
            }

        }
        return R.ok("导入成功！\n成功添加："+success+"\n成功修改："+updates);
    }
}
